<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                修改用户
            </el-header>
            <el-main>
                <el-form label-width="110px" ref="user" :rules="rules" :model="user" size="mini"
                         :label-position="formAlignLeft">
                    <el-form-item label="姓名 : " prop="name">
                        <el-input v-model="user.name" placeholder="请输入姓名 : "></el-input>
                    </el-form-item>
                    <el-form-item label="性别 : " prop="sex">
                        <el-select v-model="user.sex" placeholder="请选择性别 : " style="width: 100%;">
                            <el-option
                                    v-for="(val, key) in sexOptions"
                                    :label="val"
                                    :value="key">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="手机号 : " prop="codePhoneNumber">
                        <el-input v-model="user.phCode" placeholder="请输入手机号 : "></el-input>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        完成修改
                    </el-button>
                    <el-button @click="editOut">
                        取消修改
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {buildEditUserTask, buildEditAccountTask, buildEditJobTask, buildEditPhTask} from "../../../utils/kwpTask.js";
    import {replaceSpacesObj} from '../../../utils/webUtils.js';
    export default {
        name: "editUser",
        data() {
            return {
                formAlignLeft: 'left',
                user: {
                    uuid: this.userUuid,
                    name: '',
                    sex: '',
                    vouPhoneNumber: 'Mobile',
                    phCode: '',
                    phUuid: ''
                },
                vouers: [],
                sexOptions: {},
                rules: {
                    name: [
                        {required: true, message: '姓名不得为空', trigger: 'blur'},
                    ],
                    sex: [
                        {required: true, message: '性别不得为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        props: {
            userUuid: {
                type: String
            }
        },
        //深度监听父组件的props传值
        watch: {
            userUuid(val) {
                this.user.uuid = val;
                this.querySex();
                this.queryVouers();
            },
            other: {
                //深度观察对象
                handler(val) {
                },
                deep: true
            }
        },
        mounted() {
            this.querySex();
            this.queryVouers();
        },
        methods: {
            querySex() {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/enums/Sex",
                    params: {}
                }).then(response => {
                    this.sexOptions = response.data;
                }).catch(error => {
                    console.log(error);
                });
            },
            queryVouers() {
                this.axios.request({
                    method: "get",
                    url: "/kwp/kdi/user/" + this.user.uuid + "/vouer",
                    headers: {},
                    params: {}
                }).then(response => {
                    console.log(response);
                    this.vouers = response.data;
                    this.queryUser();
                }).catch(error => {
                    console.log(error);
                });
            },
            queryUser() {
                this.axios.request({
                    method: "get",
                    url: "/kwp/kdi/user/" + this.user.uuid,
                    headers: {},
                    params: {}
                }).then(response => {
                    this.insertUser(response.data);
                }).catch(error => {
                    console.log(error);
                });
            },
            insertUser(user) {
                this.user.name = user.name;
                this.user.sex = user.sex;
                this.user.birthday = user.birthday;
                this.user.enabledTime = user.enabledTime;
                this.user.expiryTime = user.expiryTime;
                for (let vo in this.vouers) {
                    if (this.vouers[vo].vou === 'Mobile') {
                        this.user.phCode = this.vouers[vo].code;
                        this.user.phUuid = this.vouers[vo].uuid;
                    }
                }
            },
            submit() {
                this.$refs['user'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.user = replaceSpacesObj(this.user);
                    let _this = this;
                    //修改用户基本信息
                    buildEditUserTask(this.user, _this).then(
                        (res) => {
                            _this.info = "用户新增成功。";
                            //判断用户账号的操作类型
                            let mt = "post";
                            let tas = [];
                            // let accOldVal;
                            let jobOldVal;
                            let phOldVal;
                            //取到旧的凭证值
                            for (let vo in _this.vouers) {
                                if (_this.vouers[vo].vou === 'Mobile') {
                                    phOldVal = _this.vouers[vo].code;
                                    if (phOldVal && phOldVal.length < 1) {
                                        phOldVal = null;
                                    }
                                }
                            }
                            //构建手机号任务
                            if (!phOldVal && _this.user.phCode && _this.user.phCode.length > 0) {
                                tas.push(buildEditPhTask("post", _this.user, _this));
                            } else if (phOldVal && _this.user.phCode && _this.user.phCode.length > 0) {
                                tas.push(buildEditPhTask("put", _this.user, _this));
                            } else if (phOldVal && (!_this.user.phCode || _this.user.phCode.length < 1)) {
                                tas.push(buildEditPhTask("delete", _this.user, _this));
                            }
                            return Promise.all(tas);
                        },
                        (err) => {
                            alert(err);
                            return;
                        }
                    ).then(
                        (res) => {
                            this.endProcessing();
                        },
                        (err) => {
                            this.info = _this.info + '凭证修改失败';
                            this.infoShow = true;
                        }
                    );

                });
            },
            endProcessing() {
                this.info = '修改成功,两秒后关闭';
                this.infoShow = true;
                setTimeout(() => {
                    this.infoShow = false;
                    this.$emit("editState", false);
                    this.cleanUser();
                    this.info = '';
                }, 2000);
            },
            cleanUser() {
                this.user = {
                    uuid: '',
                    name: '',
                    sex: '',
                    vouPhoneNumber: 'Mobile',
                    phCode: '',
                    phUuid: ''
                }
            },
            editOut() {
                this.infoShow = false;
                this.cleanUser();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 334px;
        height: 100%;
        z-index: 100;
    }


</style>